package com.zlzxm.shoesmarket.view.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.TestConstant;
import com.zlzxm.shoesmarket.net.RetrofitClient;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.adapter.ADHomeMenu;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityHomeMenu;
import com.zlzxm.shoesmarket.viewmodel.VMNavigationHome;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/7/21
 */
public class FMNavigationHome extends BaseFragment implements OnItemClickListener {

    private static final String TAG = "FMNavigationHome";

    private VMNavigationHome vmNavigationHome;

    private View rootView;

    ViewPager viewPager;

    ADHomeMenu adHomeMenu = new ADHomeMenu();

    RecyclerView rvMenu;

    TabLayout tabLayout;

    Banner banner;

    List<String> bannerPic = new ArrayList<>();

    BannerImageAdapter<String> bannerImageAdapter = new BannerImageAdapter<String>(bannerPic) {

        @Override
        public void onBindView(BannerImageHolder holder, String data, int position, int size) {


            Glide.with(getContext())
                    .load(RetrofitClient.BASE_URL+data)
                    .into(holder.imageView);
        }
    };

    MyPager myPager ;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        Handler handler = new Handler(Looper.getMainLooper());

        handler.post(new Runnable() {
            @Override
            public void run() {

            }
        });


        if(rootView == null){

            rootView =  inflater.inflate(R.layout.fragment_navigation_home,container,false);

        }

        return rootView;
    }


    protected void initView(@NonNull View view, @NonNull Bundle savedInstanceState) {

        viewPager = ZViewHelp.findById(view,R.id.viewPager);

        rvMenu = ZViewHelp.findById(view,R.id.rvMenu);

        tabLayout = ZViewHelp.findById(view,R.id.tabLayout);

        banner = ZViewHelp.findById(view,R.id.banner);

        if(banner.getAdapter()==null)
            banner.setAdapter(bannerImageAdapter);

        tabLayout.addTab(tabLayout.newTab().setText("热销排行榜"));

        tabLayout.addTab(tabLayout.newTab().setText("热销排行榜"));

        rvMenu.setLayoutManager(new GridLayoutManager(getContext(),4));

        tabLayout.setupWithViewPager(viewPager,false);



    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        vmNavigationHome = new ViewModelProvider(this).get(VMNavigationHome.class);
        vmNavigationHome.liveBanner.observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {

                bannerImageAdapter.setDatas(strings);
                bannerImageAdapter.notifyDataSetChanged();

            }
        });
    }



    protected void initData() {

        if(myPager == null ){

            myPager = new MyPager(getChildFragmentManager());
        }

        viewPager.setAdapter(myPager);

        rvMenu.setAdapter(adHomeMenu);

        adHomeMenu.setOnItemClickListener(this);

        vmNavigationHome.requestBanner();

    }


    @Override
    public void onResume() {
        super.onResume();
        if(banner!=null){
            Log.e(TAG,"banner start");
            banner.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(banner!=null){
            Log.e(TAG,"banner stop");
            banner.stop();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(banner!=null){
            Log.e(TAG,"banner destroy");
            banner.destroy();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

        if(adapter instanceof ADHomeMenu ){

            int id = adHomeMenu.getData().get(position).getId();

            switch (id){

                case EntityHomeMenu.HOME_MENU_MARKET_WHOLESALE:

                    ActivityIndex.startACWholesaleMarket(getContext());

                    break;

                case EntityHomeMenu.HOME_MENU_MARKET_TRANSFER:

                    ActivityIndex.startACTransferMarket(getContext());

                    break;
                case EntityHomeMenu.HOME_MENU_ADD_STATISTICS:

                    ActivityIndex.startACBugSaleAll(getContext());

                    break;
                case EntityHomeMenu.HOME_MENU_IN:

                    ActivityIndex.startACStatistic(getContext());

                    break;


                case EntityHomeMenu.HOME_MENU_OUT:

                    ActivityIndex.startACStatistic(getContext());

                    break;


                case EntityHomeMenu.HOME_MENU_ADD_GOODS:

                    ActivityIndex.startACAddGoods(getContext());

                    break;
                case EntityHomeMenu.HOME_MENU_REPOSITORY:

                    ActivityIndex.startACRepositoryManager(getContext());

                    break;

                case EntityHomeMenu.HOME_MENU_NOTE:

                    ActivityIndex.startACNote(getContext());

                    break;

                case EntityHomeMenu.HOME_MENU_MANAGE_DESC:

                    ActivityIndex.startACManageDesc(getContext());

                    break;


                case EntityHomeMenu.HOME_MENU_ADD_CALCULATE:

                    ActivityIndex.startACCalculate(getContext());

                    break;


                case EntityHomeMenu.HOME_MENU_PIC:

                    ActivityIndex.startACTendency(getContext());

                    break;


                case EntityHomeMenu.HOME_MENU_RECRUIT:

                    ActivityIndex.startACRecruit(getContext());

                    break;

            }


        }


    }


//<editor-fold defaultState="collapsed" desc="私有方法">

    private void loadBannerImage(String pic){



    }



// </editor-fold>

//<editor-fold defaultState="collapsed" desc="测试代码">




// </editor-fold>

    static class MyPager extends FragmentPagerAdapter{

        List<Fragment> pager = new ArrayList<>();

        public MyPager(@NonNull FragmentManager fm) {
            super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            pager.add(new FMHomeSort());
            pager.add(new FMHomeSort());
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return pager.get(position);
        }

        @Override
        public int getCount() {
            return pager.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ?"热销排行榜":"球鞋潜力榜";
        }
    }

}
